package Controller.Member;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import Bean.MemberBean;
import Dao.MemberDao;

public class MemberLogin implements Controller{

	private MemberDao member;

	public void setMember(MemberDao member) {
		this.member = member;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		/*String m_id = req.getParameter("m_id");
		String m_pw = req.getParameter("m_pw");
		System.out.println(m_id);
		System.out.println(m_pw);*/
		String m_id = req.getParameter("m_id");
		String m_pw = req.getParameter("m_pw");
		System.out.println(m_id);
		System.out.println(m_pw);
		ModelAndView mav = new ModelAndView();
		String comment = "";
		
		MemberBean bean = new MemberBean();
		bean.setM_id(m_id);
		bean.setM_pw(m_pw);
		int result = member.mLogin(bean);
		if (result == -1) {
			System.out.println("로그인-1 controller");
			comment = "alert('패스워드가 일치하지 않습니다.')";
			return new ModelAndView("loginError");
		} else if (result == 0) {
			System.out.println("로그인0 controller");
			comment = "alert('패스워드가 일치하지 않습니다.')";
			return new ModelAndView("loginError");
		}else {
		
			if (m_id.equals("admin")) {
				HttpSession session = req.getSession();
				session.setAttribute("member", m_id);
				mav.setViewName("sessionCheck");
				return mav;
			} else {
				HttpSession session = req.getSession();
				session.setAttribute("member", m_id);
				// memberBean mBean2 = (memberBean)
				// session.getAttribute("member");
				// System.out.println(mBean2.getMid());
				mav.setViewName("sessionCheck");
				mav.addObject(session);
				return mav;
			}
		}
	}
}
